import axios from 'axios'
import Vue from 'vue'
import { message } from 'ant-design-vue'
import '../../public/config'
import router from '../router/router'
Vue.use(message)
const VueAxios = {
  vm: {},
  // eslint-disable-next-line no-unused-vars
  install(Vue, router = {}, instance) {
    Vue.axios = instance

    Object.defineProperties(Vue.prototype, {
      axios: {
        get: function get() {
          return instance
        },
      },
      $http: {
        get: function get() {
          return instance
        },
        post: function post() {
          return instance
        },
      },
    })
  },
}
/**
 * 【指定 axios的 baseURL】
 * 如果手工指定 baseURL: '/jeecg-boot'
 * 则映射后端域名，通过 vue.config.js
 * @type {*|string}
 */
const apiBaseUrl = baseUrl
// console.log("apiBaseUrl= ",apiBaseUrl)
// 创建 axios 实例
const Axios = axios.create({
  // baseURL: '/jeecg-boot',
  baseURL: apiBaseUrl, // api base_url
  timeout: 9000, // 请求超时时间
})

// request interceptor
Axios.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem('accessToken')
    const refreshToken = sessionStorage.getItem('refreshToken')
    if (token) {
      config.headers['accessToken'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
      config.headers['refreshToken'] = refreshToken // 让每个请求携带自定义 token 请根据实际情况自行修改
    }
    // update-begin-author:taoyan date:2020707 for:多租户
    let tenantid = sessionStorage.getItem('TENANT_ID')
    if (!tenantid) {
      tenantid = 0
    }
    config.headers['tenant_id'] = tenantid
    // update-end-author:taoyan date:2020707 for:多租户
    if (config.method == 'get') {
      if (config.url.indexOf('sys/dict/getDictItems') < 0) {
        config.params = {
          _t: Date.parse(new Date()) / 1000,
          ...config.params,
        }
      }
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// function startLoading() {
//   //使用Element loading-start 方法
//   this.$store.commit('saveSpinning', true)
// }
// function endLoading() {
//   //使用Element loading-close 方法
//   this.$store.commit('saveSpinning', false)
// }

//响应拦截
Axios.interceptors.response.use(
  function(response) {
    setTimeout(() => {
      // endLoading()
    }, 300)
    if (response) {
      switch (response.data.code) {
        case 0:
          // Element.Message({
          //     message: response.data.msg,
          //     type: 'warning'
          // });
          break
        case 3000005:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          break
        case 3000001:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          break 
        case 3000002:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          break
        case 3000003:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          axios({
            method: 'get',
            url: baseUrl + '/user/userAdminToken',
            headers: {
              refreshToken: sessionStorage.getItem('refreshToken'),
            },
          })
            .then((res) => {
              if (res.data.code === 0) {
                sessionStorage.removeItem('accessToken')
                sessionStorage.setItem('accessToken', res.data.data)
                window.location.reload()
              }
            })
            .catch((error) => {
              console.log(error)
            })
          break
        case 3000004:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          break
        case 3000006:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          break
        case 4000007:
          message.warning(response.data.msg)
          sessionStorage.clear()
          router.push('/')
          break
        case 4000010:
          message.warning('异地登录，请注意您的账号安全')
          setTimeout(() => {
            sessionStorage.clear()
            router.push('/')
          }, 500)
          break
        default:
          message.error(response.data.msg)
          console.log(response.data.msg);
          break
      }
    }
    return response
  },
  function(error) {
    this.$router.push('/404')
    return Promise.reject(error)
  }
)

// post
export function postAction(url, parameter) {
  return Axios({
    url: url,
    method: 'post',
    data: parameter,
  })
}

// post method= {post | put}
export function httpAction(url, parameter, method) {
  return Axios({
    url: url,
    method: method,
    data: parameter,
  })
}

// put
export function putAction(url, parameter) {
  return Axios({
    url: url,
    method: 'put',
    data: parameter,
  })
}

// get
export function getAction(url, parameter) {
  return Axios({
    url: url,
    method: 'get',
    params: parameter,
  })
}

// deleteAction
export function deleteAction(url, parameter) {
  return Axios({
    url: url,
    method: 'delete',
    params: parameter,
  })
}
export { VueAxios }
